package com.touchX.display
{
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.Shape;
	import flash.events.Event;
	
	import webModule.product.ProductModule;
	
	public class LoaderManager extends AbstractDisplay
	{
		static public var _instance:LoaderManager;
		private var _isLoading:Boolean = false;
		private var _loadingBar:Shape;
		private var _loader:QueueLoader;
		
		public function get loader():QueueLoader{return _loader;}
		public function LoaderManager(singleton:Singleton)
		{
			super();
			visible=false;
		}
		
		public function createLoader():void{
			if(_loader!=null) _loader.dispose();
			_loader=null;
			_loader=new QueueLoader();
			_loader.addEventListener(QueueLoaderEvent.QUEUE_START, onQueueStart, false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.ITEM_START, onItemStart, false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.ITEM_PROGRESS, onItemProgress, false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE, onItemComplete,false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.ITEM_ERROR, onItemError,false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.QUEUE_PROGRESS, onQueueProgress, false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE, onQueueComplete,false, 0, true);
			_loader.addEventListener(QueueLoaderEvent.ITEM_HTTP_STATUS, onHTTPError,false, 0, true);
		}
		private function onQueueStart(event:QueueLoaderEvent):void {
			//trace("onQueueStart >> event.type:"+event.type);
			_isLoading = true;
			this.visible=true;
		}
		private function onItemStart(event:QueueLoaderEvent):void {
			//trace("\n onItemStart>> event.type:"+event.type, ",item title: "+event.title);
		}
		private function onItemProgress(event:QueueLoaderEvent):void {
			//trace("\n onItemProgress>> event.percentage:"+event.percentage,"title:"+event.title);
		}
		private function onQueueProgress(event:QueueLoaderEvent):void {
			//	trace("\n onQueueProgress>> event.queuepercentage:"+event.queuepercentage,"title:"+event.title);
			if(_loadingBar) _loadingBar.width=stage.stageWidth*event.queuepercentage;
		}
		
		private function onQueueComplete(event:QueueLoaderEvent):void {
			//trace("onQueueComplete ** "+event.type);
			//if(_targetListener!=null) _targetListener(event);
			_isLoading  = _loader.isLoading();
			this.visible=_isLoading;
			
		}
		private function onItemComplete(event:QueueLoaderEvent):void {
			if(event.info.target) event.info.target.onItemComplete(event);
		}
		private function onItemError(event:QueueLoaderEvent):void {
			//trace("\n>>"+event.title + ": " + event.message + "\n");
		}
		private function onHTTPError(event:QueueLoaderEvent):void {
			//trace("\n\t\t>>"+event.message+"\n");
		}
		
		protected override function resize(event:Event=null):void{
			super.resize(event);
			createLoadingBar();
		}
		private function createLoadingBar():void{
			graphics.clear();
			graphics.beginFill(0xcccccc);
			graphics.drawRect(0,0,stage.stageWidth,2);
			graphics.endFill();
			if(!_loadingBar){
				_loadingBar=new Shape();
				_loadingBar.graphics.beginFill(0xff0000);
				_loadingBar.graphics.drawRect(0,0,stage.stageWidth,1);
				_loadingBar.graphics.endFill();
				addChild(_loadingBar);
			}else{
				if(!_isLoading) _loadingBar.width=stage.stageWidth;
			}
		}
		static public function getInstance():LoaderManager{
			if(_instance==null){
				_instance=new LoaderManager(new Singleton());
			}
			return _instance;
		}
	}
}

class Singleton{};